package com.nc.delivery_project.server.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.nc.delivery_project.server.dao.AbstractDao;
import com.nc.delivery_project.server.dao.StateProvinceDao;
import com.nc.delivery_project.server.dao.UserDao;
import com.nc.delivery_project.server.db_util.ConnectionManager;
import com.nc.delivery_project.server.domain.City;
import com.nc.delivery_project.server.domain.Country;
import com.nc.delivery_project.server.domain.Role;
import com.nc.delivery_project.server.domain.StateProvince;
import com.nc.delivery_project.server.domain.User;
import com.nc.delivery_project.server.util.CRUD;

public class StateProvinceService  extends BaseService<StateProvince>{
	
	
	public StateProvinceService() {
		this(new StateProvinceDao());
	}
	
	
	public StateProvinceService(AbstractDao<StateProvince> statePrDao) {
		super(statePrDao);
	}


	public StateProvinceService(Connection connection, boolean isAutoCloseConnection) {
        super(new StateProvinceDao(), connection, isAutoCloseConnection);
    }



	@Override
	public List<StateProvince> read() {
		List<StateProvince> statePrs = null;
        try {
        	Connection conn = getConnection();
            dao.setConnection(conn);
            statePrs = dao.read();
            
            CountryService countryService = new CountryService(conn, false);
       
            for (StateProvince state : statePrs) {
            		state.setCountry(countryService.read(state.getCountry().getCountryId()));
                
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return statePrs;
	}

	@Override
	public StateProvince read(int id){
		StateProvince statePr = null;
        try {
        	 Connection conn = getConnection();
             dao.setConnection(conn);
             statePr = dao.read(id);
           
            if (statePr != null) {
                Country stateCountry = new CountryService(conn, false).read(statePr.getCountry().getCountryId());
                if (stateCountry != null) {
                	statePr.getCountry().setCountryTitle(stateCountry.getCountryTitle());
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return statePr;
	}


    public List<City> loadStateProvinceCity(int statePrId) throws SQLException {
        List<City> cities = new CityService().loadCityByStatePrId(statePrId);
        return cities;
    }
    

	public List<StateProvince> loadStatePrByCountryId(int countryId) {
		List<StateProvince> statePrs = null;
        try {
        	Connection conn = getConnection();
            dao.setConnection(conn);
            statePrs = ((StateProvinceDao)dao).readByCountryId(countryId);
            
            CountryService countryService = new CountryService(conn, false);
       
            for (StateProvince state : statePrs) {
            		state.setCountry(countryService.read(state.getCountry().getCountryId()));
                
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return statePrs;
	}
	
}
